<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('配置文件类型')" append-to-body :v-if="dialogVisible" :visible.sync="dialogVisible" width="600px" top="5%" @close='close'>
    <el-table ref="multipleTable" height="500" :data="typeAllList" tooltip-effect="dark" @selection-change="handleChange" size='mini'>
      <el-table-column type="selection" align='center' width="100"></el-table-column>
      <el-table-column prop="type" align='left' :label="$t('文件类型')">
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="innerAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="innerAddClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
// import { getBlacklistFileTypeList } from '@/api/safe/black'
import typeArray from './typeArray'
export default {
  name: 'BlackFileBatchDialog',
  data () {
    return {
      dialogVisible: false,
      typeAllList: typeArray,
      typeList: [],
      record: {},
      name: null
    }
  },
  methods: {
    init (data, record) {
      this.dialogVisible = true
      this.record = record
      this.name = data.proto

      // getBlacklistFileTypeList().then(res => {
      //   if (res.status === 0) {
      //     this.typeAllList = res.info
      //     console.log(this.typeAllList)
      //   }
      // })
      this.$nextTick(() => {
        if (data.typeList) {
          var list = []
          for (let val of data.typeList) {
            for (let value of this.typeAllList) {
              if (val.type === value.type) {
                list.push(value)
                break
              }
            }
          }
          list.forEach(item => {
            this.$refs.multipleTable.toggleRowSelection(item, true)
          })
        }
        if (data.type === '0') {
          this.typeList = this.typeAllList
          this.typeList.forEach(item => {
            this.$refs.multipleTable.toggleRowSelection(item, true)
          })
        }
      })
    },
    submit () {
      if (this.typeList && this.typeList.length > 0) {
        this.$emit('typeChange', this.name, this.typeList, this.typeAllList)
        this.close()
      } else {
        this.$messages.error(this.$t('文件类型不能为空！'))
      }
    },
    close () {
      this.dialogVisible = false
      this.typeList = []
      this.record = {}
      this.name = null
      this.$refs.multipleTable.clearSelection()
    },
    handleChange (data) {
      this.typeList = data
    }
  }
}
</script>
